using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace TimeLogger
{
    public partial class MainForm : Form
    {
        private TimeLogDocument _doc;
        private string _filename;
        DataCollections _events;
        BasecampManager _bcm;
        
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            _filename = Application.UserAppDataPath + "\\reminders.xml";
            _doc = File.Exists(_filename) ? TimeLogDocument.Open(_filename) : new TimeLogDocument(_filename);

            _bcm = new BasecampManager(
                Properties.Settings.Default.BasecampHost,
                Properties.Settings.Default.BasecampUsername,
                Properties.Settings.Default.BasecampPassword,
                false);

            TimeEntryLists();

            if (_doc.Projects.Count == 0)
            {
                IList<Project> projects = _bcm.GetProjects();
                foreach (Project project in projects)
                {
                    OfflineProject p = new OfflineProject();
                    p.Id = project.ID;
                    p.Name = project.Name;
                    _doc.Projects.Add(p);
                }
                _doc.ForceSave();
            }

            this.cbProjectDetails.DisplayMember = "Name";
            this.cbProjectDetails.ValueMember = "Id";
            this.cbProjectDetails.DataSource = _doc.Projects;            
        }

        private void miOptions_Click(object sender, EventArgs e)
        {
            OptionsForm frm = new OptionsForm();
            frm.ShowDialog();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {            
            _events = new DataCollections();
            _events.Name = cbProjectDetails.Text;
            _events.ProjectID = int.Parse(cbProjectDetails.SelectedValue.ToString());
            if (txtTime.Text == "")
                _events.Time = 0;
            else _events.Time = Convert.ToDouble(txtTime.Text);
            _events.Message = txtMessage.Text;
            _events.Date = Convert.ToDateTime(dtTimePicker.Text);                
            _doc.AddEvent(_events);                
            
            TimeEntryLists();
            btnUpdate.Enabled = false;
        }
        
        private void Save()
        {
            this._doc.Save();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            this.cbProjectDetails.DataSource = _doc.Projects;
            dtTimePicker.Text = DateTime.Now.ToString();
            txtTime.Text = "";
            txtMessage.Text = "";
            TimeEntryLists();
            _events = null;            
            btnUpdate.Enabled = false;
        }

        private void lstviewDatas_DoubleClick(object sender, EventArgs e)
        {
            if (lstviewDatas.SelectedItems.Count > 0)
            {
                ListViewItem item = lstviewDatas.SelectedItems[0];
                _events = (DataCollections)item.Tag;

                cbProjectDetails.Text = _events.Name;
                txtTime.Text = _events.Time.ToString();
                txtMessage.Text = _events.Message;
                dtTimePicker.Text = _events.Date.ToString();
                btnUpdate.Enabled = true;
            }
        }

        private void MainForm_Move(object sender, EventArgs e)
        {
            if (this == null)
            {
                return;
            }
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Hide();
            }
            else
            {
                this.Show();
            }
        }

        private void notifier_MouseClick(object sender, MouseEventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Show();
                this.WindowState = FormWindowState.Normal;
            }
            this.Activate();
            this.Focus();
        }

        private void notifier_MouseDoubleClick(object sender, MouseEventArgs e)
        {

            this.Show();
            this.WindowState = FormWindowState.Normal;
        }

        public void TimeEntryLists()
        {
            lstviewDatas.Items.Clear();
            foreach (DataCollections evt in _doc.Events)
            {
                string[] item = new string[4];
                item[0] = evt.Name;
                item[1] = evt.Date.ToShortDateString();
                item[2] = evt.Time.ToString();
                item[3] = evt.Message;
                ListViewItem list = new ListViewItem(item);
                list.Tag = evt;
                lstviewDatas.Items.Add(list);
            }
            if (_doc.Events.Count == 0)            
                btnUpload.Enabled = false;            
            else btnUpload.Enabled = true;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            ListView.SelectedListViewItemCollection events = lstviewDatas.SelectedItems;
            foreach (ListViewItem evnt in events)
            {
                DataCollections evt = (DataCollections)evnt.Tag;                
                _doc.RemoveEvent(evt);
                lstviewDatas.Items.Clear();
                TimeEntryLists();
            }
        }

        private void btnUpload_Click(object sender, EventArgs e)
        {
            ListView.SelectedListViewItemCollection events = lstviewDatas.SelectedItems;
            if(events.Count>0)
            {
                foreach (ListViewItem evnt in events)
                {
                    DataCollections evt = (DataCollections)evnt.Tag;
                    bool success = _bcm.CreateTimeTrack(evt.ProjectID, PersonId, evt.Date, evt.Time.ToString(), evt.Message);
                    if (success)
                    {
                        _doc.RemoveEvent(evt);
                    }
                }
            }
            else
            {
                foreach (ListViewItem evnt in lstviewDatas.Items)
                {
                    DataCollections evt = (DataCollections)evnt.Tag;
                    bool success = _bcm.CreateTimeTrack(evt.ProjectID, PersonId, evt.Date, evt.Time.ToString(), evt.Message);
                    if (success)
                    {
                        _doc.RemoveEvent(evt);
                    }
                }
            }
            TimeEntryLists();
        }
        private int PersonId
        {
            get { return Properties.Settings.Default.BasecampPersonId; }
        }

        private void miExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void miUpdate_Click(object sender, EventArgs e)
        {
            IList<Project> projects = _bcm.GetProjects();
            _doc.Projects.Clear();
            foreach (Project project in projects)
            {
                OfflineProject p = new OfflineProject();
                p.Id = project.ID;
                p.Name = project.Name;
                _doc.Projects.Add(p);
            }
            _doc.ForceSave();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            _events.Name = cbProjectDetails.Text;
            _events.ProjectID = int.Parse(cbProjectDetails.SelectedValue.ToString());
            if (txtTime.Text == "")
                _events.Time = 0;
            else _events.Time = Convert.ToDouble(txtTime.Text);
            _events.Message = txtMessage.Text;
            _events.Date = Convert.ToDateTime(dtTimePicker.Text);
            _doc.SetDirty();
            _doc.Save();
            TimeEntryLists();

            btnUpdate.Enabled = false;
        }

        private void showWindowToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
        }

        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            notifier.Visible = false;
            Application.Exit();
        }

    }
}